<!--  -->
<template>
  <div class="Home">
    <h2 class="title">来人统计</h2>
    <div class="allBox">
      <div class="chooseText">
        <span
          :class="idx === index ? 'curr' : ''"
          v-for="(item, index) in topData"
          :key="index"
          @click="topChooseClick(item, index)">
          {{ item }}
        </span>
        <p style="line-height: 30px; margin-left: 50px">自定义：</p>
        <DatePicker
          type="daterange"
          :clearable="false"
          :value="timeVal"
          @on-change="dataChoose"
          ref="oselect"
          :options="options"
          placeholder="请选择时间"
          style="width: 220px"></DatePicker>
      </div>
      <div style="margin: 10px 0" v-if="idx != null">日期：{{ dateFormat }}</div>
      <div class="allData">
        <div class="allDataBox">
          <span>
            {{ statistical.visitorCount }}
            <br />
            总来访数
          </span>
          <span>
            {{ statistical.newComerCount }}
            <br />
            新客来访数
          </span>
          <span>
            {{ statistical.oldComerCount }}
            <br />
            老客复访数
          </span>
          <span>
            {{ statistical.clComerCount }}
            <br />
            线索转到访数
          </span>
        </div>
        <div class="flex" style="margin-top: 20px">
          <Button type="primary" style="margin-right: 8px" @click="exportFun" v-if="unlocking('0301010000')">
            导出
          </Button>
          <Button type="primary" v-if="btnFlag" @click="goBack">返回上一级</Button>
        </div>
      </div>
      <dt-grid ref="table" :select="{ sort: sortCase }" :page="page" :onFetch="onFetch" />
    </div>
  </div>
</template>
<script>
  import homeMgr from '@/biz/buildingHome/home'
  import buildMgr from '@/biz/building'
  import caseMgr from '@/biz/crimeSceneList'
  import workMgr from '@/biz/work/index'
  import { buildPage } from '@/router/buildPage.js'
  import moment from 'moment'
  import store from '@/store'
  import visitColumn from './opt/column'
  export default {
    components: {},
    data() {
      return {
        btnFlag: false,
        options: {
          disabledDate(date) {
            return date && date.valueOf() < new Date().getTime() - 365 * 2 * 24 * 3600 * 1000
          }
        },
        topData: ['昨日', '今日', '本周', '本月', '本年'],
        idx: 1,
        startTime: '',
        endTime: '',
        loading: false,
        page: {
          total: 0
        },
        chooseTime: {},
        parameter: {},
        statistical: {},
        orderBy: '',
        orderField: '',
        timeVal: [],
        filterParams: {}
      }
    },
    computed: {
      dateFormat() {
        console.log('233333333333')
        let date = ''
        if (this.chooseTime.startTime == this.chooseTime.endTime) {
          date = moment(this.chooseTime.startTime).format('YYYY-MM-DD')
        } else {
          date = `${moment(this.chooseTime.startTime).format('YYYY-MM-DD')} 至 ${moment(
            this.chooseTime.endTime
          ).format('YYYY-MM-DD')}`
        }
        return date
      }
    },
    watch: {},
    methods: {
      goBack() {
        let oldDate = JSON.parse(this.$tool.getStorage('parameter'))
        this.parameter.orgId = oldDate.orgId
        this.parameter.level = oldDate.level
        this.btnFlag = false
        this.$refs.table.init(this, new visitColumn(this))
        this.$refs.table.fetch()
      },
      viewFun(val) {
        this.parameter.orgId = val.orgId
        this.parameter.level = val.level
        if (this.parameter.level < 4 && this.parameter.level > 1) {
          this.btnFlag = true
          this.$refs.table.init(this, new visitColumn(this))
          this.$refs.table.fetch()
        } else if (this.parameter.level == 4) {
          this.btnFlag = false
          let params = {
            housesId: val.orgId
          }
          caseMgr.buildinghousesList(params).then((res) => {
            let item = { ...res.list[0] }
            // 更新案场
            buildMgr.updateBuildInfo(item)
            this.$tool.setStorage({ caseBuildingId: JSON.stringify(item.housesId) })
            store.dispatch('setRouterList', buildPage)
            this.$router.push({
              path: '/buildingHome/statistics'
            })
          })
        } else {
          this.btnFlag = false
        }
      },
      sortCase(sort) {
        this.orderBy = sort.order ? sort.order : ''
        this.orderField = sort.prop.split('_').pop()
        this.$refs.table.fetch()
      },
      // 切换日期
      topChooseClick(item, index) {
        this.idx = index
        this.dataText = item
        this.$refs.oselect && this.$refs.oselect.handleClear() // 清空自定义时间
        this.chooseTime = homeMgr.resovleTime(index)
        this.$refs.table.clearSort()
        this.$refs.table.fetch(true)
      },
      dataChoose(val) {
        if (val[0] != '' && val[1] != '') {
          this.idx = null
          this.chooseTime = {
            startTime: val[0],
            endTime: val[1]
          }

          this.$refs.table.fetch()
        }
      },

      //访客详情分页查询
      onFetch(data) {
        let params = {
          ...data,
          level: this.parameter.level,
          orgId: this.parameter.orgId,
          startTime: this.chooseTime.startTime,
          endTime: this.chooseTime.endTime,
          orderField: this.orderField, //排序
          orderBy: this.orderBy //正序倒序
        }
        this.filterParams = { ...params }
        return workMgr
          .comerDayList(params)
          .then((res) => {
            this.statistical = res.value
            this.page.total = res.pages
            return res.list
          })
          .catch((err) => {})
      },
      exportFun() {
        let params = {
          ...this.filterParams
        }
        workMgr.comerDayListExport(params)
      }
    },
    created() {
      if (this.$route.params.orgId) {
        this.$tool.setStorage({ parameter: JSON.stringify(this.$route.params) })
      }
      this.parameter = JSON.parse(this.$tool.getStorage('parameter'))
      if (this.parameter.endTime && this.parameter.startTime) {
        this.chooseTime.startTime = this.parameter.startTime
        this.chooseTime.endTime = this.parameter.endTime
      } else {
        this.chooseTime = homeMgr.resovleTime(1)
      }
      console.log(this.chooseTime, 'time')
      if (this.parameter.checkIndex) {
        this.idx = this.parameter.checkIndex
      } else {
        this.timeVal = [this.chooseTime.startTime, this.chooseTime.endTime]
        this.idx = null
      }
    },
    mounted() {
      this.$refs.table.init(this, new visitColumn(this))
      this.$refs.table.fetch()
    }
  }
</script>
<style lang="scss" scoped>
  .Home {
    padding: 20px;

    .allBox {
      margin-top: 20px;

      .chooseText {
        margin: 10px 0;
        height: 40px;
        border-radius: 2px;
        padding: 5px;
        box-sizing: border-box;
        display: flex;
        flex-direction: row;

        span {
          height: 30px;
          border: #dcdee2 1px solid;
          width: 90px;
          text-align: center;
          line-height: 30px;
          font-size: 14px;
          color: #666;
          cursor: pointer;
          margin-right: 5px;
        }

        span.curr {
          border: #2d8cf0 1px solid;
          color: #2d8cf0;
        }
      }

      .tab {
        margin-top: 20px;
      }
      .allData {
        display: flex;
        flex-direction: row;
        justify-content: space-between;

        .allDataBox {
          display: flex;
          flex-direction: row;
          width: 500px;
          background: #e8eaec;
          justify-content: space-around;
          padding: 15px 0;
          margin: 10px 0;
          span {
            width: 100px;
            display: block;
            text-align: center;
            font-weight: bold;
            font-size: 16px;
          }
        }
      }
    }
  }
</style>
